package com.sky.controller.user;

import com.sky.pojo.AddressBook;
import com.sky.result.Result;
import com.sky.service.user.AddressBookService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@Api(tags = "地址簿功能")
@RequestMapping("/user/addressBook")
public class AddressBookController {
    @Autowired
    private AddressBookService addressBookService;

    @PostMapping
    @ApiOperation("添加地址")
    public Result insertAddressBook(@RequestBody AddressBook addressBook) {
        log.info("接收到的地址数据{}", addressBook);
        addressBookService.insertAddressBook(addressBook);
        return Result.success();
    }

    @GetMapping("/list")
    @ApiOperation("列表查询地址")
    public Result<List<AddressBook>> queryAddressBookList() {
        log.info("查询所有的地址数据");
        List<AddressBook> list = addressBookService.queryAddressBookList();
        return Result.success(list);
    }

    @GetMapping("/default")
    @ApiOperation("查询设置默认地址")
    public Result<AddressBook> queryAcquiesceAddressBook() {
        log.info("设置默查询认地址的");
        AddressBook addressBook = addressBookService.queryAcquiesceAddressBook();
        if (addressBook != null) {
            return Result.success(addressBook);
        }
        return Result.error("没有查询到默认地址");
    }

    @GetMapping("/{id}")
    @ApiOperation("根据id查询地址")
    public Result<AddressBook> queryAddressBookId(@PathVariable("id") Long id) {
        log.info("根据id查询地址：{}", id);
        AddressBook addressBook = addressBookService.queryAddressBookId(id);
        return Result.success(addressBook);
    }

    @PutMapping
    @ApiOperation("根据修改地址")
    public Result updateAddressBook(@RequestBody AddressBook addressBook) {
        log.info("根据修改地址：{}", addressBook);
        addressBookService.updateAddressBook(addressBook);
        return Result.success();
    }

    @DeleteMapping
    @ApiOperation("根据id删除地址")
    public Result deleteAddressBook(Long id) {
        log.info("根据id删除地址：{}", id);
        addressBookService.deleteAddressBook(id);
        return Result.success();
    }

    @PutMapping("/default")
    @ApiOperation("设置默认地址")
    public Result setAcquiesceAddressBook(@RequestBody AddressBook addressBook) {
        log.info("设置默认地址：{}", addressBook);
        Long id = addressBook.getId();
        addressBookService.setAcquiesceAddressBook(id);
        return Result.success();
    }
}
